<html>
<head><meta charset="utf-8"><title>Thoughts about specialized MIR bodies · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html">Thoughts about specialized MIR bodies</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222478569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222478569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222478569">(Jan 12 2021 at 19:02)</a>:</h4>
<p>I randomly looked at how Option::map is generated into MIR, and that gave me some thoughts that I would be interested in bouncing here. Consider this Godbolt setup <a href="https://godbolt.org/z/nxjEz3">https://godbolt.org/z/nxjEz3</a><br>
It contains two rust implementations of the map function. One is restricted to <code>F: Copy + FnOnce(T) -&gt; U</code>, while the other is just <code>F: FnOnce(T) -&gt; U</code>. Since nothings needs to be dropped in the first impl, we don't generate any <code>drop</code>s which simplifies the MIR body of map_copy somewhat, compared to map. (The unoptimized LLVM is also simpler)</p>
<p>Now my thought is if it would be worthwhile to have a scheme with multiple variants of a MIR function with Copy, Non-Copy versions. Then we could dispatch on the "best" variation at the call-site. So if we call Option&lt;T&gt;::map with a Copy closure, then we call the Copy variant. Inlining <code>map</code>would also work of course, but the above scheme would also work for functions that are not inlined.</p>



<a name="222478911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222478911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222478911">(Jan 12 2021 at 19:04)</a>:</h4>
<p>Can we use <em>actual</em> impl specialization for this?</p>



<a name="222480211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222480211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222480211">(Jan 12 2021 at 19:13)</a>:</h4>
<p>I have zero experience with specialization, sorry. Does a specialization always have to be for a trait?</p>



<a name="222480407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222480407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222480407">(Jan 12 2021 at 19:15)</a>:</h4>
<p>yeah</p>



<a name="222480419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222480419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222480419">(Jan 12 2021 at 19:15)</a>:</h4>
<p>but it's used a lot already</p>



<a name="222480522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222480522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222480522">(Jan 12 2021 at 19:15)</a>:</h4>
<p>and if the <code>drop</code> calls are actually what's causing the slowdown, it should be possible to avoid them by just specializing on <code>Copy</code></p>



<a name="222480733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222480733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222480733">(Jan 12 2021 at 19:17)</a>:</h4>
<p>My only metric has been looking at lines of MIR and LLVM-IR produced, so it's probably not a slowdown. But if the MIR can be improved for such a core function that would be nice</p>



<a name="222480956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222480956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222480956">(Jan 12 2021 at 19:19)</a>:</h4>
<p>well if it has no practical impact that seems like a pointless exercise</p>



<a name="222481081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222481081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222481081">(Jan 12 2021 at 19:20)</a>:</h4>
<p>I'm not saying it's pointless, i'm just saying I don't have any data backing that it will be a small/large improvement/regression</p>



<a name="222485873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222485873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222485873">(Jan 12 2021 at 19:57)</a>:</h4>
<p>Is still similar to what you meant? <a href="https://godbolt.org/z/crTxW9">https://godbolt.org/z/crTxW9</a><br>
It does generate two MIR bodies so as long the the correct spezialization is chosen at the call site, then it doesn't have to deal with drop</p>



<a name="222485998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222485998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222485998">(Jan 12 2021 at 19:58)</a>:</h4>
<p>looks about right, yeah</p>



<a name="222488692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts%20about%20specialized%20MIR%20bodies/near/222488692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Thoughts.20about.20specialized.20MIR.20bodies.html#222488692">(Jan 12 2021 at 20:20)</a>:</h4>
<p>Just doing this specialization on Option::map manually is probably not doing much in the grand scheme of things, but i would imagine there's quite a few functions where this could have some use. What are your thoughts on a MIR opt pass that creates auto specializations for call-sites?</p>
<ul>
<li>If a fn has T as a bound, but is called with T:Copy, then create that T:Copy specialization that does not need drops</li>
<li>If a fn has T:Clone as a bound, but it is called with T:Copy, then create the T:Copy specialization that does not need .clone()</li>
</ul>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>